<?php

namespace App\Admin\Controllers\Sys;

use App\Models\Sys\AdsCategory;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class AdsCategoryController extends Controller
{
    use ModelForm;
    public $header = '广告位管理';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(AdsCategory::class, function (Grid $grid) {

            $grid->model()->orderBy('id','desc');
            $grid->id('编号')->sortable();
            $grid->name('名称');
            $grid->code('代码');

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(AdsCategory::class, function (Form $form) {
            $form->text('name', '名称');
            $form->text('code', '代码')
                ->creationRules(['required', "unique:jxt_sys_ads_category"])
                ->updateRules(['required', "unique:jxt_sys_ads_category,code,{{id}}"]);

            $form->disableViewCheck();
            $form->tools(function (Form\Tools $tools){
                $tools->disableView();
            });
        });
    }

}
